import moment, { Moment } from 'moment';

/**
 * 日历组件工具类
 *
 * @export
 * @class CalendarUtil
 */
export class CalendarUtil {

    /**
     * 星期英文数组
     *
     * @type {string[]}
     * @memberof CalendarUtil
     */
    public readonly week: string[] = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'];

    /**
     * 星期对应文本值
     *
     * @memberof CalendarUtil
     */
    public readonly weekText = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];

    /**
     * 传入「1~7」表示「星期一 ~ 星期天」
     *
     * @param {number} num
     * @returns {string}
     * @memberof CalendarUtil
     */
    public getWeekText(num: number): string {
        return this.weekText[num];
    }

    /**
     * 计算天起始时间
     *
     * @param {string} date 日期
     * @param {number} timeDeviation 时间偏差
     * @returns {Moment}
     * @memberof CalendarUtil
     */
    public getDayStartDate(date: string, timeDeviation: number): Moment {
        return moment(new Date(date + ' 00:00:00')).add(timeDeviation <= 720 ? -timeDeviation : timeDeviation, 'minutes');
    }

    /**
     * 计算天结束时间
     *
     * @param {string} date
     * @param {number} timeDeviation
     * @returns {Moment}
     * @memberof CalendarUtil
     */
    public getDayEndDate(date: string, timeDeviation: number): Moment {
        return moment(new Date(date + ' 23:59:59')).add(1, 'seconds').add(timeDeviation <= 720 ? -timeDeviation : timeDeviation, 'minutes');
    }

    /**
     * 是否在指定时间范围内有数据重叠
     *
     * @param {Moment} start 范围一，起始时间
     * @param {Moment} end 范围一，结束时间
     * @param {Moment} comparisonStartData 范围二，起始时间
     * @param {Moment} comparisonEndData 范围二，结束时间
     * @returns {boolean}
     * @memberof CalendarUtil
     */
    public isOverlapsTime(start: Moment, end: Moment, comparisonStartData: Moment, comparisonEndData: Moment): boolean {
        if (start && start.isBetween(comparisonStartData, comparisonEndData)) {
            return true;
        }
        if (end && end.isBetween(comparisonStartData, comparisonEndData)) {
            return true;
        }
        if (comparisonStartData.isBetween(start, end)) {
            return true;
        }
        if (comparisonEndData.isBetween(start, end)) {
            return true;
        }
        return false;
    }

    /**
     * 自增标识
     *
     * @protected
     * @type {number}
     * @memberof CalendarUtil
     */
    protected autoPrimaryKey: number = 0;

}
// 导出实例
export const calendarUtil = new CalendarUtil();